<?php
namespace app\admin\model;

use think\Model;
use think\facade\Cache;
use think\facade\Session;
use think\facade\Request;
use think\facade\Config;
use think\facade\Cookie;

class Admin extends Model {
    //
    protected $pk = 'aid';
    //登入
    public function login($username, $password, $remember){
        $ip = Request::instance()->ip();
        $name = 'fail-'.$ip;
        $times = 10;//失败次数
        $expire = 3600;//时间段
        $fail = Cache::get($name, 0);
        if($fail > 10){
            return ['code' => 1, 'msg' => '你已经连续登录失败'.$times.'次，'.intval($expire / 60).'分钟内禁止再次登录!'];
        }
        $user = $this->where(['username' => $username, 'status' => 1])->find();
        if(!$user){
            Cache::has($name) ? Cache::inc($name) : Cache::set($name, 1, $expire);
            return ['code' => 1, 'msg' => '账号不存在或被禁用!'];
        }
        $user = $user->toArray();
        if(Cookie::has('password', 'admin_')){
            $cookie_password = Cookie::get('password', 'admin_');
            if(substr($cookie_password, 0, 10) == $password){
                $password = $cookie_password;
            }
        }
        if(md6($password) != $user['password'] && $password != $user['password']){
            Cache::has($name) ? Cache::inc($name) : Cache::set($name, 1, $expire);
            return ['code' => 1, 'msg' => '密码错误!'];
        }
        $token = $this->authToken($user);
        Cache::has($name) ? Cache::rm($name) : null;
        if($remember){//设置cookie
            $configArr = ['prefix' => 'admin_', 'expire' => 3600 * 24 * 10];
            Cookie::set('username', $username, $configArr);
            Cookie::set('password', $user['password'], $configArr);
        }else{
            Cookie::has('username', 'admin_') ? Cookie::delete('username', 'admin_') : null;
            Cookie::has('password', 'admin_') ? Cookie::delete('password', 'admin_') : null;
        }
        return ['code' => 0, 'msg' => '登录成功!'];
    }
    //
    private function authToken($user){
        $token = Config::get('app.session_name');
        unset($user['password']);
        Session::set($token, $user);
        $log = [
            'aid' => $user['aid'],
            'ip' => Request::instance()->ip(),
            'token' => session_id(),
            'agent' => Request::instance()->header('user-agent'),
            'addtime' => time(),
        ];
        model('AdminLog')->data($log)->save();
        return true;
    }
    //设置密码
    public function resetPassword($data, $aid){
        $oldpass = $this->where(['aid' => $aid])->value('password');
        if($oldpass != md6($data['oldPassword'])){
            return ['code' => 1, 'msg' => '旧密码不正确'];
        }
        $res = $this->update(['password' => md6($data['password'])], ['aid' => $aid]);
        if($res){
            return ['code' => 0, 'msg' => '修改成功'];
        }
        return ['code' => 1, 'msg' => '修改失败'];
    }
    //登出
    public function logout($session_name){
        Session::has($session_name) ? Session::delete($session_name) : null;
    }
}
